package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.Login_RegisterActivity;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.adapter.FloorInoAdapter;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.FloorInfoListBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.iphoneDialogBuilder;

import java.util.List;

public class FloorInfoActivity extends BaseActivity implements View.OnClickListener, ContributorView {
    private TextView fragment_jdts_title;
    private ImageView fragment_jdts_back;//返回按钮
    private TextView fragment_lyxx_fabu;
    private RelativeLayout fragment_lyxx_sousuo;//楼宇搜索
    private PullToRefreshListView fragment_lyxx_listView;
    private Context context;
    private String name;
    private ContributorPresenter contributorPresenter;
    private ListView actualListView;
    private int skip = 0;
    private final int limit = 6;
    private boolean isfresh = true;
    private final String BUILDING_LIST_FLAG = "BUILDING_LIST_FLAG";
    private List<FloorInfoListBean> floorInfoList;
    private FloorInoAdapter floorInoAdapter;
    private MyApplication myApplication;
    private String cidNumber;
    private RelativeLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.floorinfo_layout);
        context = this;
        initView();
        myApplication=(MyApplication) getApplication();
        contributorPresenter = new ContributorPresenter(this, context);
        Intent intent=getIntent();
        name = intent.getStringExtra("name");
        cidNumber=intent.getStringExtra("id");
        fragment_jdts_title.setText(name);
        showData();
    }

    private void initView() {
        fragment_jdts_title = (TextView) findViewById(R.id.fragment_jdts_title);
        fragment_jdts_back = (ImageView) findViewById(R.id.fragment_jdts_back);//返回按钮
        fragment_lyxx_fabu = (TextView) findViewById(R.id.fragment_lyxx_fabu);
        fragment_lyxx_sousuo = (RelativeLayout) findViewById(R.id.fragment_lyxx_sousuo);//楼宇搜索
        fragment_lyxx_listView = (PullToRefreshListView) findViewById(R.id.fragment_lyxx_listView);
        fragment_jdts_back.setOnClickListener(this);
        fragment_lyxx_fabu.setOnClickListener(this);
        fragment_lyxx_sousuo.setOnClickListener(this);
        layout=(RelativeLayout) findViewById(R.id.layout);
        fragment_lyxx_listView.setMode(PullToRefreshBase.Mode.BOTH);
        actualListView = fragment_lyxx_listView.getRefreshableView();

        actualListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        actualListView.setDivider(null);
        Tools.showRefreshView(fragment_lyxx_listView);

        fragment_lyxx_listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                // TODO Auto-generated method stub
                // Update the LastUpdatedLabe
                // 处理下拉刷新的业务
                if (refreshView.getCurrentMode().equals(PullToRefreshBase.Mode.PULL_FROM_START)) {
                    isfresh = true;
                    Tools.showRefreshView(fragment_lyxx_listView);
                    skip = 0;
                    showData();
                } else {
                    isfresh = false;
                    Tools.showRefreshView(fragment_lyxx_listView);
                    skip += limit;
                    showData();
                }

            }
        });

        actualListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (floorInfoList != null && floorInfoList.size() > 0) {
                    Intent intent = new Intent(context, FloorInfoDetailActivity.class);
                    intent.putExtra("bid", floorInfoList.get(position-1).getId());
                    intent.putExtra("classname",FloorInfoActivity.class.getSimpleName());
                    startActivity(intent);
                }
            }
        });

    }

    /**
     * @Description 服务器获取数据
     * @author jaily.zhang
     */
    private void showData() {
        contributorPresenter.getBuildingList(cidNumber, skip, limit, BUILDING_LIST_FLAG);
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(BUILDING_LIST_FLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<FloorInfoListBean> floorInfoListBeanList = JsonTools.jsonStringToList(jsonMessage, FloorInfoListBean.class);
                    if (floorInfoListBeanList != null && floorInfoListBeanList.size() > 0) {
                        if (isfresh) {
                            layout.setVisibility(View.GONE);
                            floorInfoList = floorInfoListBeanList;
                            floorInoAdapter = new FloorInoAdapter(context, floorInfoList);
                            actualListView.setAdapter(floorInoAdapter);
                        } else {
                            layout.setVisibility(View.GONE);
                            floorInfoList.addAll(floorInfoListBeanList);
                            floorInoAdapter.notifyDataSetChanged();
                        }

                    }else
                    {
                        if(isfresh)
                        {
                            layout.setVisibility(View.VISIBLE);
                        }
                    }

                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
        fragment_lyxx_listView.onRefreshComplete();
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_jdts_back:
                finish();
                break;
            case R.id.fragment_lyxx_fabu:
                boolean isLogin=Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                if(isLogin)
                {
                    Intent intent=new Intent(context,FloorPublishActivity.class);
                    intent.putExtra("cid",cidNumber);
                    startActivityForResult(intent,0);
                }else
                {
                    iphoneDialogBuilder idb = new iphoneDialogBuilder(context);
                    idb.setTitle("提示");
                    idb.setMessage("是否登录");
                    idb.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(context, Login_RegisterActivity.class);
                            myApplication.setSourceIntoLoginRegisterActivity(FloorInfoActivity.class.getSimpleName());
                            startActivity(intent);

                        }
                    });
                    idb.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    idb.show();
                }
                break;
            case R.id.fragment_lyxx_sousuo:
                Intent intent=new Intent(context,FloorSearchActivity.class);
                intent.putExtra("cid",cidNumber);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0 && resultCode==1)
        {
            isfresh = true;
            skip = 0;
            showData();
        }

    }
}
